angular.module('wineIndexcontrollers', [])
  //酒款首页
  .controller('WineIndexCtrl', function($scope,$state,$timeout,$ionicSlideBoxDelegate,$cordovaBarcodeScanner,$ionicScrollDelegate,Storage,ENV,Wine,History,Loading,Toast){

    var memberId = Storage.get("member").member_id;
    $scope.picPath = ENV.picPath;
    /*酒款列表*/
    $scope.allWineList = [];//所有酒款
    $scope.comentWineList = [];//评论优先
    $scope.collectWineList = [];//收藏优先
    /*国家列表*/
    $scope.countryList = [];
    /*酒款类型列表*/
    $scope.typeList = [];
    /*酒款试饮场合列表*/
    $scope.occasionList = [];
    //底部全选状态
    $scope.bottomCheckBoxSelected = false;
    //已选择的酒款数量
    $scope.selectWineCount = 0;


    /*******************************************/
    $scope.slideChanged = function(index){
      if(index==0){
        showAllFun();
      }else if(index==1){
        showBCommentFun();
      }else if(index==2){
        showBCollectFun();
      }else if(index==3){
        showBDetailFun();
      }
    }
    function changeTab(){
      $scope.bottomCheckBoxSelected = false;
      $scope.selectWineCount = 0;
      for(i=0;i<$scope.allWineList.length;i++){
        $scope.allWineList[i].selected = false;
      }
      for(i=0;i<$scope.comentWineList.length;i++){
        $scope.comentWineList[i].selected = false;
      }
      for(i=0;i<$scope.collectWineList.length;i++){
        $scope.collectWineList[i].selected = false;
      }
    }


    $scope.showWhich = [true,false,false,false];
    $scope.selectAllBottom = function(){
      if($scope.bottomCheckBoxSelected){//本来是选中状态
        $scope.selectWineCount = 0;
        if($scope.showWhich[0]){//全选全部酒款
          for(i=0;i<$scope.allWineList.length;i++){
            $scope.allWineList[i].selected = false;
          }
        }else if($scope.showWhich[1]){//全选评论优先
          for(i=0;i<$scope.comentWineList.length;i++){
            $scope.comentWineList[i].selected = false;
          }
        }else if($scope.showWhich[2]){//全选收藏优先
          for(i=0;i<$scope.collectWineList.length;i++){
            $scope.collectWineList[i].selected = false;
          }
        }
      }else{
        if($scope.showWhich[0]){//全选全部酒款
          $scope.selectWineCount = $scope.allWineList.length;
          for(i=0;i<$scope.allWineList.length;i++){
            $scope.allWineList[i].selected = true;
          }
        }else if($scope.showWhich[1]){//全选评论优先
          $scope.selectWineCount = $scope.comentWineList.length;
          for(i=0;i<$scope.comentWineList.length;i++){
            $scope.comentWineList[i].selected = true;
          }
        }else if($scope.showWhich[2]){//全选收藏优先
          $scope.selectWineCount = $scope.collectWineList.length;
          for(i=0;i<$scope.collectWineList.length;i++){
            $scope.collectWineList[i].selected = true;
          }
        }
      }
      $scope.bottomCheckBoxSelected = !$scope.bottomCheckBoxSelected;
    }

   /* $scope.loadMore = function(){
      if($scope.showWhich[0]){
          Wine.getMoreAllWineList();
      }else if($scope.showWhich[1]){
          Wine.getMoreCommentFWineList();
      }else if($scope.showWhich[2]){
          Wine.getMoreCollectFWineList();
      }
    }*/
    //全部酒款加载更多
    $scope.loadMoreAll = function(){Wine.getMoreAllWineList();}
    //评论优先加载更多
    $scope.loadMoreComment = function(){Wine.getMoreCommentFWineList();}
    //收藏优先加载更多
    $scope.loadMoreCollect = function(){Wine.getMoreCollectFWineList();}
    //全部酒款是否有下一页
    $scope.allWineHasNextPage = function(){
      return Wine.allWineHasNextPage();
    }
    //评论优先酒款是否有下一页
    $scope.commentFHasNextPage = function(){
      return Wine.commentFHasNextPage();
    }
    //收藏优先酒款是否有下一页
    $scope.collectFHasNextPage = function(){
      return Wine.collectFHasNextPage();
    }



    /*********************显示全部开始******************************/
    //获取全部酒列表
    Loading.showLoading();
    Wine.getWineList();
    $scope.showAll =function(){
      $ionicSlideBoxDelegate.slide(0,300);
      showAllFun();
    };
    showAllFun = function(){
      $ionicScrollDelegate.scrollTop(true);
      $scope.showWhich = [true,false,false,false];
      changeTab();
    }
    //访问网络成功的时候返回的数据---->获取所有的酒款
    $scope.$on('Wine.WineListUpdated',function(){
      Loading.hideLoading();
      $scope.allWineList = Wine.getAllWineListJson();
      for(var i=0;i<$scope.allWineList.length;i++){
        $scope.allWineList[i].selected = false;
      }
    });
    //加载更多全部酒款
    $scope.$on('Wine.MoreWineListUpdated',function() {
      $scope.$broadcast('scroll.infiniteScrollComplete');
      var moreWineList = Wine.getAllWineListJson();
      $scope.allWineList = $scope.allWineList.concat(moreWineList);
      //底部的全选复选框变回未选择
      if(moreWineList.length>0){
        $scope.bottomCheckBoxSelected = false;
      }
    });
    $scope.selectAllWine = function(index){
      if($scope.allWineList[index].selected){
        $scope.selectWineCount--;
        $scope.bottomCheckBoxSelected = false;
      }else{
        $scope.selectWineCount++;
      }
      $scope.allWineList[index].selected = !$scope.allWineList[index].selected;
    }
    /*********************显示全部结束******************************/
    /*********************显示评论优先开始******************************/
    var commentFirst = true;
    $scope.showBComment =function(){
      $ionicSlideBoxDelegate.slide(1,300);
      showBCommentFun();
    };
    showBCommentFun = function(){
      $ionicScrollDelegate.scrollTop();
      $scope.showWhich = [false,true,false,false];
      changeTab();
      if(commentFirst){
        Loading.showLoading();
        Wine.getWineList('wine_comment_num');
        commentFirst = false;
      }
    }
    //访问网络成功的时候返回的数据----->获取评论优先的所有酒款
    $scope.$on('Wine.CommentWineListUpdated',function(){
      Loading.hideLoading();
      $scope.comentWineList = Wine.getCommentFWineListJson();
    });
    //加载更多评论优先的酒款
    $scope.$on('Wine.MoreCommentWineListUpdated',function() {
      $scope.$broadcast('scroll.infiniteScrollComplete');
      var moreWineList = Wine.getCommentFWineListJson();
      $scope.comentWineList = $scope.comentWineList.concat(moreWineList);
      //底部的全选复选框变回未选择
      if(moreWineList.length>0){
        $scope.bottomCheckBoxSelected = false;
      }
    });
    $scope.selectCommentWine = function(index){
      if($scope.comentWineList[index].selected){
        $scope.selectWineCount--;
        $scope.bottomCheckBoxSelected = false;
      }else{
        $scope.selectWineCount++;
      }
      $scope.comentWineList[index].selected = !$scope.comentWineList[index].selected;
    }
    /*********************显示评论优先结束******************************/
    /*********************显示收藏优先开始******************************/
    var collectFirst = true;
    $scope.showBCollect =function(){
      $ionicSlideBoxDelegate.slide(2,300);
      showBCollectFun();
    };
    showBCollectFun = function(){
      $ionicScrollDelegate.scrollTop();
      $scope.showWhich = [false,false,true,false];
      changeTab();
      if(collectFirst){
        Loading.showLoading();
        Wine.getWineList('wine_collect_num');
        collectFirst = false;
      }
    }
    //访问网络成功的时候返回的数据------>获取收藏优先的所有酒款
    $scope.$on('Wine.CollectWineListUpdated',function(){
      Loading.hideLoading();
      $scope.collectWineList = Wine.getCollectFWineListJson();
    });
    //加载更多收藏优先的酒款
    $scope.$on('Wine.MoreCollectWineListUpdated',function() {
      $scope.$broadcast('scroll.infiniteScrollComplete');
      var moreWineList = Wine.getCollectFWineListJson();
      $scope.collectWineList = $scope.collectWineList.concat(moreWineList);
      //底部的全选复选框变回未选择
      if(moreWineList.length>0){
        $scope.bottomCheckBoxSelected = false;
      }
    });
    $scope.selectCollectWine = function(index){
      if($scope.collectWineList[index].selected){
        $scope.selectWineCount--;
        $scope.bottomCheckBoxSelected = false;
      }else{
        $scope.selectWineCount++;
      }
      $scope.collectWineList[index].selected = !$scope.collectWineList[index].selected;
    }
    /*********************显示收藏优先结束******************************/



    /*********************显示精细查找开始******************************/
    $scope.$on('$ionicView.beforeEnter',function(){
      for(var i=0;i<$scope.countryList.length;i++){
        $scope.countryList[i].selected = false;
      }
      for(var j=0;j<$scope.typeList.length;j++){
        $scope.typeList[j].selected = false;
      }
      for(var k=0;k<$scope.occasionList.length;k++){
        $scope.occasionList[k].selected = false;
      }
      for(var m=0;m<$scope.priceList.length;m++){
        $scope.priceList[m].selected = false;
      }
    });
    $scope.priceList = [
      {id:0,text:'100元以下',selected:false},
      {id:1,text:'100-500元',selected:false},
      {id:2,text:'500-1000元',selected:false},
      {id:3,text:'1000-3000元',selected:false},
      {id:4,text:'3000元以上',selected:false}
    ];
    var detailFirst = true;
    $scope.showBDetail =function(){
      $ionicSlideBoxDelegate.slide(3,300);
      showBDetailFun();
    };
    showBDetailFun = function(){
      $ionicScrollDelegate.scrollTop();
      $scope.showWhich = [false,false,false,true];
      changeTab();
      if(detailFirst){
        Loading.showLoading();
        Wine.getSearchDetail();
        detailFirst = false;
      }else{//将选择的去掉
        for(var i=0;i<$scope.countryList.length;i++){
          $scope.countryList[i].selected = false;
        }
        for(var j=0;j<$scope.typeList.length;j++){
          $scope.typeList[j].selected = false;
        }
        for(var k=0;k<$scope.occasionList.length;k++){
          $scope.occasionList[k].selected = false;
        }
        for(var m=0;m<$scope.priceList.length;m++){
          $scope.priceList[m].selected = false;
        }
      }
    }
    //当获取搜索的详细信息的时候调用该方法
    $scope.$on('Wine.WineSearchDetailUpdated',function(){
      Loading.hideLoading();
      var result = Wine.getSearchDetailJson();
      //获取到所有的类型
      $scope.countryList = result.countryList;
      $scope.typeList = result.wineTypeList;
      $scope.occasionList = result.situationList;
      for(var i=0;i<$scope.countryList.length;i++){
        $scope.countryList[i].selected = false;
      }
      for(var j=0;j<$scope.typeList.length;j++){
        $scope.typeList[j].selected = false;
      }
      for(var k=0;k<$scope.occasionList.length;k++){
        $scope.occasionList[k].selected = false;
      }
    });
    //选择国家
    $scope.selectCountry = function(index){
      $scope.countryList[index].selected = !$scope.countryList[index].selected;
    }
    //选择类型
    $scope.selectType = function(index){
      $scope.typeList[index].selected = !$scope.typeList[index].selected;
    }
    //选择饮用场合
    $scope.selectOccasion = function(index){
      $scope.occasionList[index].selected = !$scope.occasionList[index].selected;
    }
    //选择价格范围
    $scope.selectedMoney = function(index){
      var temp = $scope.priceList[index].selected;
      for(var i=0;i<$scope.priceList.length;i++){
        $scope.priceList[i].selected = false;
      }
      $scope.priceList[index].selected = !temp;
    }
    //展开或者收起精细查找
    $scope.isOpens = [
      {count:4,text:'展开',isOpen:false},
      {count:4,text:'展开',isOpen:false},
      {count:4,text:'展开',isOpen:false},
      {count:3,text:'展开',isOpen:false}
      ];
    $scope.extend = function(index){
      $scope.isOpens[index].isOpen = !$scope.isOpens[index].isOpen;
      if($scope.isOpens[index].isOpen){//展开的时候
        $scope.isOpens[index].text='展开';
        $scope.isOpens[index].count=4;
        if(index==3){
          $scope.isOpens[index].count=3;
        }
      }else{//未展开
        $scope.isOpens[index].text='收起';
        if(index==0){$scope.isOpens[index].count=$scope.countryList.length;}//国家
        else if(index==1){$scope.isOpens[index].count=$scope.typeList.length;}//类型
        else if(index==2){$scope.isOpens[index].count=$scope.occasionList.length;}//试饮场合
        else if(index==3){$scope.isOpens[index].count=$scope.priceList.length;}//价格区间
      }
    };
    //点击完成之后
    $scope.searchByDetail = function(){
      var countryIds = [];
      var typeIds = [];
      var occasionIds = [];
      var priceId = 100;
      for(var i=0;i<$scope.countryList.length;i++){
        if($scope.countryList[i].selected){
          countryIds.push($scope.countryList[i].country_code);
        }
      }
      for(var j=0;j<$scope.typeList.length;j++){
        if($scope.typeList[j].selected){
          typeIds.push($scope.typeList[j].type_id);
        }
      }
      for(var m=0;m<$scope.occasionList.length;m++){
        if($scope.occasionList[m].selected){
          occasionIds.push($scope.occasionList[m].situation_id);
        }
      }
      for(var n=0;n<$scope.priceList.length;n++){
        if($scope.priceList[n].selected){
          priceId = $scope.priceList[n].id;
        }
      }
      if(countryIds.length==0&&typeIds.length==0&&occasionIds.length==0&&priceId==100){
        Toast.showMessage("请选择精细搜索的条件");
      }else{
        //精细搜索
        Wine.getWineListByDetail(countryIds,typeIds,occasionIds,priceId,'price','desc');
        var searchDetail = {'countryIds':countryIds,'typeIds':typeIds,'occasionIds':occasionIds,'priceId':priceId};
        Storage.set('searchDetail',searchDetail);//保存精细搜索的信息
        //跳转到搜索页面
        $state.go("wineSearch");
        Loading.showLoading();
      }
    }
      /*********************显示精细查找结束******************************/
    /******************其他功能********************/
    //返回
    $scope.goback = function(){
      History.goback();
    };
    //输入款获取焦点的时候，跳转到搜索页面
    $scope.go2search = function(){
        $state.go('wineSearch');
    };
    //加入推荐酒单
    $scope.addToWineRecommend = function(){
        var recommendInfo = [];
        if($scope.showWhich[0]){//所有酒款
          for(i=0;i<$scope.allWineList.length;i++){
            if($scope.allWineList[i].selected){
               temp ={
                wine_id:$scope.allWineList[i].wine_id,
                member_id:memberId
              };
              recommendInfo.push(temp);
            }
          }
        }else if($scope.showWhich[1]){//评论优先
          for(i=0;i<$scope.comentWineList.length;i++){
            if($scope.comentWineList[i].selected){
              temp ={
                wine_id:$scope.comentWineList[i].wine_id,
                member_id:memberId
              };
              recommendInfo.push(temp);
            }
          }
        }else if($scope.showWhich[2]){//收藏优先
          for(i=0;i<$scope.collectWineList.length;i++){
            if($scope.collectWineList[i].selected){
              temp ={
                wine_id:$scope.collectWineList[i].wine_id,
                member_id:memberId
              };
              recommendInfo.push(temp);
            }
          }
        }
      //加入推荐酒单
      if(recommendInfo && recommendInfo.length!=0){
        Wine.add2WineRecommend(recommendInfo);
      }else{
        Toast.showMessage("请选择酒款");
      }
    };
    $scope.$on('Wine.add2WineRecommendUpdated',function(){
      var result = Wine.getAddWineRecommendJson();
        if($scope.showWhich[0]){//所有酒款
          for(i=0;i<$scope.allWineList.length;i++){
            if($scope.allWineList[i].selected){
              $scope.allWineList[i].selected = false;
            }
          }
        }else if($scope.showWhich[1]){//评论优先
          for(i=0;i<$scope.comentWineList.length;i++){
            if($scope.comentWineList[i].selected){
              $scope.comentWineList[i].selected = false;
            }
          }
        }else if($scope.showWhich[2]){//收藏优先
          for(i=0;i<$scope.collectWineList.length;i++){
            if($scope.collectWineList[i].selected){
              $scope.collectWineList[i].selected = false;
            }
          }
        }
        $scope.bottomCheckBoxSelected = false;
        $scope.selectWineCount = 0;
      Toast.showMessage(result.insertWineMsg);
    });

    //改变数字的显示，添加逗号分割
    $scope.getFormatNum = function (num) {
      return formatNum(num);
    };

  })
//酒款搜索的Controller
.controller('WineSearchCtrl',function($scope,$state,$ionicScrollDelegate,Wine,ENV,History,Storage,Toast,Loading,MeRecommendOrderList,MeHistoryList){
    var memberId = Storage.get("member").member_id;
    var searchTip  = '';//精细搜索还是关键字搜索
    var orderBy = 'price';
    var order = 'desc';
    $scope.picPath = ENV.picPath;
    $scope.isFirstIn = true;
    $scope.wineList = [];
    $scope.selectWineCount=0;
    $scope.bottomCheckBoxSelected = false;

    //切换上方的选项卡
    $scope.showWhich = [true,false,false];
    $scope.showMoreIcon = [true,true,true];
    $scope.priceTip = '价格高到低';
    $scope.commentTip = '评论多到少';
    $scope.collectTip = '收藏多到少';
    changeTab = function(){
      $scope.priceTip = '价格高到低';$scope.commentTip = '评论多到少';$scope.collectTip = '收藏多到少';
      $ionicScrollDelegate.scrollTop();
    }
    selectWineByOption = function(orderBy,order){
      Loading.showLoading();
      if(searchTip=='searchByDetail'){
        var searchDetail = Storage.get("searchDetail");
        Wine.getWineListByDetail(searchDetail.countryIds,searchDetail.typeIds,searchDetail.occasionIds,searchDetail.priceId,orderBy,order);
      }else if(searchTip=='searchByKey'){
        Wine.getMmWineListByKey($scope.searchKey,orderBy,order);
      }
    }
    $scope.changePrice = function(){
      changeTab();
      orderBy = 'price';
      if($scope.showWhich[0]){
        if($scope.showMoreIcon[0]){
          $scope.priceTip = '价格低到高';
          order = 'asc';
        }else{
          $scope.priceTip = '价格高到低';
          order = 'desc';
        }
        $scope.showMoreIcon[0] = !$scope.showMoreIcon[0];
      }else{
        $scope.showMoreIcon[0] = true;
      }
      $scope.showMoreIcon[1]=$scope.showMoreIcon[2] = true;
      $scope.showWhich = [true,false,false];
      selectWineByOption(orderBy,order);
    }
    $scope.changeComment = function(){
      changeTab();
      orderBy = 'comment';
      if($scope.showWhich[1]){
        if($scope.showMoreIcon[1]){
          $scope.commentTip = '评论少到多';
          order = 'asc';
        }else{
          $scope.commentTip = '评论多到少';
          order = 'desc';
        }
        $scope.showMoreIcon[1] = !$scope.showMoreIcon[1];
      }else{
        $scope.showMoreIcon[1] = true;
      }
      $scope.showMoreIcon[0]=$scope.showMoreIcon[2] = true;
      $scope.showWhich = [false,true,false];
      selectWineByOption(orderBy,order);
    }
    $scope.changeCollect = function(){
      changeTab();
      orderBy = 'collect';
      if($scope.showWhich[2]){
        if($scope.showMoreIcon[2]){
          $scope.collectTip = '收藏少到多';
          order = 'asc';
        }else{
          $scope.collectTip = '收藏多到少';
          order = 'desc';
        }
        $scope.showMoreIcon[2] = !$scope.showMoreIcon[2];
      }else{
        $scope.showMoreIcon[2] = true;
      }
      $scope.showMoreIcon[0]=$scope.showMoreIcon[1] = true;
      $scope.showWhich = [false,false,true];
      selectWineByOption(orderBy,order);
    }
    //全选酒款
    $scope.selectWine = function(index){
      $scope.wineList[index].selected=!$scope.wineList[index].selected;
      if($scope.wineList[index].selected){
        $scope.selectWineCount++;
        if($scope.selectWineCount==$scope.wineList.length){
          $scope.bottomCheckBoxSelected = true;
        }
      }else{
        $scope.selectWineCount--;
        $scope.bottomCheckBoxSelected = false;
      }
    }
    //点击全选后
    $scope.selectAllBottom = function(){
      if($scope.bottomCheckBoxSelected){
          for(i=0;i<$scope.wineList.length;i++){
            $scope.wineList[i].selected = false;
          }
        $scope.selectWineCount=0;
      }else{
        for(i=0;i<$scope.wineList.length;i++){
          $scope.wineList[i].selected = true;
        }
        $scope.selectWineCount=$scope.wineList.length;
      }
      $scope.bottomCheckBoxSelected = !$scope.bottomCheckBoxSelected;

    }
    //直接生成推荐酒单
    $scope.createRecommendWine = function(){
      var createReWineList = [];
      for(i=0;i<$scope.wineList.length;i++){
        if($scope.wineList[i].selected){
          createReWineList.push($scope.wineList[i]);
        }
      }
      if(createReWineList.length==0){
        Toast.showMessage("请选择酒款");
      }else{
        MeRecommendOrderList.setSelectWineList(createReWineList);
        for(i=0;i<$scope.wineList.length;i++){
          if($scope.wineList[i].selected){
            $scope.wineList[i].selected = false;
          }
        }
        $state.go('recommend-order-list');
        $scope.selectWineCount=0;
        $scope.bottomCheckBoxSelected = false;
        MeHistoryList.setGo2CreateRe(true);
      }
    }
    //扫描酒标搜索酒款
    $scope.$on('Wine.WineSearchUpdated',function(){
      Loading.hideLoading();
      $scope.wineList = Wine.getWineListJson().wineList;
      $scope.wineListCount = $scope.wineList.length;
    });
    //精细查找酒款显示
    $scope.$on('Wine.WineSearchByDetailUpdated',function(){
      Loading.hideLoading();
      searchTip = 'searchByDetail';
      $scope.wineList = Wine.getWineListByDetailJson();
      $scope.wineListCount = Wine.getMmWineListCount();
    });
    //加载更多精细查找显示
    $scope.$on('Wine.MoreWineSearchByDetailUpdated',function(){
      $scope.$broadcast('scroll.infiniteScrollComplete');
      var moreWineList = Wine.getWineListByDetailJson();
      $scope.wineList = $scope.wineList.concat(moreWineList);
      if(moreWineList.length>0){
        $scope.bottomCheckBoxSelected = false;
      }
    });
    //加入推荐酒单
    $scope.addToWineRecommend = function(){
      var recommendInfo = [];
      for(i=0;i<$scope.wineList.length;i++){
        if($scope.wineList[i].selected){
          temp ={
            wine_id:$scope.wineList[i].wine_id,
            member_id:memberId
          };
          recommendInfo.push(temp);
        }
      }
      //加入推荐酒单
      if(recommendInfo && recommendInfo.length!=0){
          Wine.add2WineRecommend(recommendInfo);
      }else{
        Toast.showMessage("请选择酒款");
      }
    }
    $scope.$on('Wine.add2WineRecommendUpdated',function(){
      var result = Wine.getAddWineRecommendJson();
      Toast.showMessage(result.insertWineMsg);
      for(i=0;i<$scope.wineList.length;i++){
        if($scope.wineList[i].selected){
          $scope.wineList[i].selected = false;
        }
      }
      $scope.selectWineCount=0;
      $scope.bottomCheckBoxSelected = false;
    });
      //搜索的关键字
      $scope.searchKey = '';
      //搜索
      $scope.search = function(){
        $ionicScrollDelegate.scrollTop();
        $scope.bottomCheckBoxSelected = false;
        $scope.isFirstIn = false;
        if($scope.searchKey == ''){
            Toast.showMessage("请输入关键字搜索酒款");
        }else{
          Loading.showLoading();
          Wine.getMmWineListByKey($scope.searchKey,orderBy,order);
          $scope.selectWineCount = 0;
         // $scope.searchKey = '';
        }
      };
    //加载更多
    $scope.loadMore = function(){
      if(searchTip=='searchByDetail'){
        var searchDetail = Storage.get("searchDetail");
        Wine.getMoreWineListByDetail(searchDetail.countryIds,searchDetail.typeIds,searchDetail.occasionIds,searchDetail.priceId,orderBy,order);
      }else if(searchTip=='searchByKey'){
        Wine.getMoreMmWineListByKey($scope.searchKey,orderBy,order);
      }
    }
    //是否下一页
    $scope.hasNextPage = function(){
      return Wine.mmWineHasNextPage();
    }
    //精细查找是否有下一页
    $scope.detailSearchHasNextPage = function(){
      return Wine.detailSearchHasNextPage();
    }
    //访问网络成功的时候返回的数据
    $scope.$on('Wine.MmWineListByKeyUpdated',function(){
      Loading.hideLoading();
      searchTip = 'searchByKey';
      $scope.wineList = Wine.getMmWineListJson();
      $scope.wineListCount = Wine.getMmWineListCount();
    });
    $scope.$on('Wine.MoreMmWineListByKeyUpdated',function() {
      $scope.$broadcast('scroll.infiniteScrollComplete');
      var moreWineList = Wine.getMmWineListJson();
      $scope.wineList = $scope.wineList.concat(moreWineList);
      //底部的全选复选框变回未选择
      if(moreWineList.length>0){
        $scope.bottomCheckBoxSelected = false;
      }
    });
    $scope.goback = function(){
      History.goback();
    }

    //改变数字的显示，添加逗号分割
    $scope.getFormatNum = function (num) {
      return formatNum(num);
    };
  })




  //扫描酒标
  .controller('BarCodeSearchCtrl',function($scope,$state,$timeout,$cordovaBarcodeScanner,Wine,ENV,History,Storage,Toast){

      $scope.wine={
        barCode : ''
      }
      //手动输入
      $scope.scan = function(){
          if($scope.wine.barCode ==''){
            Toast.showMessage("请输入或条形码或者扫一扫");
          }else{
            Wine.getWineByBarCode($scope.wine.barCode);
            $state.transitionTo('wineSearch');
          }
      }
    //扫描条形码
    var haveClick = false;
    $scope.openScanBarcode = function(){
      if(!haveClick){
        haveClick = true;
        $cordovaBarcodeScanner.scan().then(function(imageData) {
         // alert(imageData.text);
          if(imageData.text==null || imageData.text==''){
            Toast.showMessage("取消扫描");
          }else{
            Wine.getWineByBarCode(imageData.text);
            $state.transitionTo('wineSearch');
          }
        }, function(error) {
          Toast.showMessage("扫描条形码失败");
        });
      }
      $timeout(function(){
        haveClick = false;
      },500);
    }

      $scope.goback  = function(){
        History.goback();
      }
  });
